<template>
  <el-drawer :title="title" :visible.sync="visible" append-to-body class="drawer-container" size="800px">
    <div class="drawer-search">
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入角色名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="queryParams.code" clearable placeholder="请输入权限字符"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="drawer-content">
      <div class="drawer-operate">
        <el-button :disabled="multiple" icon="el-icon-delete" plain size="mini" type="danger" @click="handleClear">
          彻底删除
        </el-button>
        <el-button :disabled="multiple" icon="el-icon-s-promotion" plain size="mini" type="primary"
                   @click="handleRestor">还原
        </el-button>
        <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleClean">清空回收站</el-button>
      </div>
      <div class="drawer-data">
        <el-table ref="table" v-loading="loading" row-key="id" :data="dataList"
                  @selection-change="handleSelectionChange">
          <el-table-column :reserve-selection="true" type="selection" width="55"></el-table-column>
          <el-table-column v-for="item in columns" align="center" :label="item.label" :prop="item.props"
                           :width="item.width" show-overflow-tooltip>
            <template slot-scope="scope">
              <dict-tag v-if="item.dictable" :options="dicts[item.code]" :value="scope.row[item.props]"
                        :show-label="item.tag"/>
              <span v-else>{{ scope.row[item.props] }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button icon="el-icon-s-promotion" size="mini" type="text" @click="handleRestor(scope.row)">还原
              </el-button>
              <el-button icon="el-icon-delete" size="mini" type="text" @click="handleClear(scope.row)">彻底删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="pageParam.total > 0" :total="pageParam.total" :page.sync="pageParam.pageNum"
                    :limit.sync="pageParam.pageSize"/>
      </div>
    </div>
  </el-drawer>
</template>
<script>

import {clean, clear, recycle, restore} from "../../api/base";

export default {
  name: "RecycleCard",
  props: {
    dicts: Object,
    columns: Array,
    forms: Array,
    baseUrl: String,
  },
  data() {
    return {
      ids: [],
      title: '',
      visible: false,
      loading: false,
      multiple: true,
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {}
    }
  },
  created() {
  },
  methods: {
    getList() {
      this.loading = true;
      recycle(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
        this.dataList = response.rows
        this.pageParam.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.pageParam.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
    handleClear(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认彻底删除选择的数据？').then(() => {
        clear(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {

        });
      });
    },
    handleRestor(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认还原选择的数据？').then(() => {
        restore(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("还原成功");
        }).catch(() => {

        });
      });
    },
    handleClean() {
      this.$modal.confirm('是否确认清空所有已删除数据？').then(() => {
        clean(this.baseUrl).then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {

        });
      });
    },
  }
}
</script>
<style scoped lang="scss">

</style>
